package com.seventh.parkingrent.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seventh.parkingrent.common.Result;
import com.seventh.parkingrent.domain.ParkingSpace;
import com.seventh.parkingrent.domain.RentMessage;
import com.seventh.parkingrent.domain.Vo.ParkingSpaceVo;
import com.seventh.parkingrent.service.impl.ParkingSpaceServiceImpl;
import com.seventh.parkingrent.service.impl.RentMessageServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车位信息表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@RestController
@RequestMapping("")
@CrossOrigin("*")
@Slf4j
public class ParkingSpaceController {

    @Autowired
    ParkingSpaceServiceImpl parkingSpaceService;
    @Autowired
    RentMessageServiceImpl rentMessageService;


    @GetMapping("/findstatus")
    public Result findStatus(int parkingSpaceId){
    ParkingSpace parkingSpace =parkingSpaceService.findStatus(parkingSpaceId);
        return Result.success(parkingSpace);
    }

    @GetMapping("/findparkingspacebyid")
    public Result findParkingSpaceById(int parkingSpaceId){

        ParkingSpace parkingSpace =parkingSpaceService.findParkingSpaceById(parkingSpaceId);
        return Result.success(parkingSpace);
    }

    @GetMapping("/delparkingspace")
    public Result delParkingSpace(int parkingSpaceId){

        Integer i =parkingSpaceService.delParkingSpace(parkingSpaceId);
        return i!=null? Result.success("成功") : null;
    }

    @GetMapping("/findallparkingspace")
    public Result findAll(int userId,int current){

        IPage<ParkingSpace> list =parkingSpaceService.findAll(userId,current);
        return Result.success(list);
    }

    @GetMapping("/changestate")
    public Result changeState(String state,int parkingSpaceId) {
        Integer result = parkingSpaceService.changeState(state,parkingSpaceId);
        if (result != null) {
            return Result.success("成功");
        } else {
            return null;
        }
    }

    @GetMapping("/checkno")
    public Result checkno(String parkingSpaceNo) {
        List<ParkingSpace>  result = parkingSpaceService.findAllByNo(parkingSpaceNo);
        if (result.size()!= 0) {
            return Result.fail("已经被添加");
        } else {
            return Result.success("该车位未被添加");
        }
    }


    @PostMapping("/addparkingspace")
    public Result addParkingSpace(@RequestBody ParkingSpace parkingSpace){

       Boolean bool= parkingSpaceService.addParkingSpace(parkingSpace);
       if (bool){
           return Result.success("成功");
       }else {
           return Result.fail("失败");
       }

     }

    @GetMapping("/getParkingSpace")
    public Result getParkingSpace(Integer pageIndex,Integer pageSize){
        log.info("分页查询停车位信息得到当前页:{},页容量:{}",pageIndex,pageSize);
        IPage parkingSpace = parkingSpaceService.getParkingSpace(pageIndex, pageSize);
        //根据停车位id去订单表查询出租时间
//        List<ParkingSpace> records = parkingSpace.getRecords();
//        List<RentMessage> rentMessages = rentMessageService.getRentByPSId(records);
//        List<ParkingSpaceVo> parkingSpaceVos = new ArrayList<>();
//        for (int i=0;i<records.size();i++){
//            ParkingSpaceVo parkingSpaceVo = new ParkingSpaceVo();
//            ParkingSpace space = records.get(i);
//            RentMessage rentMessage = rentMessages.get(i);
//            parkingSpaceVo.setParkingSpaceAddr(space.getParkingSpaceAddr());
//            parkingSpaceVo.setParkingSpacePic(space.getParkingSpacePic());
//            parkingSpaceVo.setPointX(space.getPointX());
//            parkingSpaceVo.setPointY(space.getPointY());
//            parkingSpaceVo.setRentStartTime(rentMessage.getRentStartTime());
//            parkingSpaceVo.setRentEndTime(rentMessage.getRentEndTime());
//            parkingSpaceVos.add(parkingSpaceVo);
//        }
        return Result.success(parkingSpace);
    }

    @GetMapping("nearbyparking")
    public Result nearbyparking(Double x,Double y){
        log.info("根据经纬度筛选附近最优车位得到x:{},得到y:{}",x,y);
        List<RentMessage> rentMessages = parkingSpaceService.nearbyparking(x, y);
        List<ParkingSpaceVo> parkingSpaceVos = new ArrayList<>();
        for (RentMessage rentMessage : rentMessages) {
            //创建一个返回对象Vo,订单信息赋值
            ParkingSpaceVo parkingSpaceVo = new ParkingSpaceVo();
            parkingSpaceVo.setRentStartTime(rentMessage.getRentStartTime());
            parkingSpaceVo.setRentEndTime(rentMessage.getRentEndTime());
            parkingSpaceVo.setCost(rentMessage.getCost());
            parkingSpaceVo.setLessorId(rentMessage.getLessorId());
            parkingSpaceVo.setRentMessageId(rentMessage.getRentMessageId());
            Integer id = rentMessage.getSpacingSpaceId();
            log.info("id:{}",id);
            //根据停车场id获取停车场信息
            ParkingSpace parkingSpace = parkingSpaceService.getParkingSpaceByid(id);
            log.info("parkingSpace:{}",parkingSpace);
            //把需要的信息赋给返回对象,车位信息赋值
            parkingSpaceVo.setPointY(parkingSpace.getPointY());
            parkingSpaceVo.setPointX(parkingSpace.getPointX());
            parkingSpaceVo.setParkingSpaceAddr(parkingSpace.getParkingSpaceAddr());
            parkingSpaceVo.setParkingSpacePic(parkingSpace.getParkingSpacePic());
            parkingSpaceVos.add(parkingSpaceVo);
        }
        return Result.success(parkingSpaceVos);
    }

}

